<template>
  <div class="bgh" style="min-height:100vh">
    <div class="mui-content-padded bgf" style="margin:0">
      <div class="mui-segmented-control">
        <a class="mui-control-item mui-active" href="#payfored">已支付</a>
        <a class="mui-control-item" href="#payfor">待支付</a>
      </div>
    </div>
    <div id="payfored" class="mui-control-content  mui-active">
      <div>
        <div class="mui-input-row mui-search" style="height:40px;">
          <input
            v-model="queryname"
            type="search"
            class="mui-input-clear"
            style="background:#fff;border-radius:0;padding-left:15px"
            placeholder="搜索"
          />
        </div>
        <div
          style="margin:10px 0;background:#fff;padding:20px"
          v-for="(item, index) in detaillists2"
          :key="index"
          @click="goiddetail(item)"
        >
          <img
            
            :src="item.imagepath"
            alt
            style="width:80px;height:80px;float:left;margin-right:15px"
          />
          <span style="display:inline-block;width:70%;padding:10px 0;" class="detail_span fs16">
            <span class="detail_span fs16" style="width:100%">
              {{item.itemname}}
              <span style="display:inline-block;float:right">{{item.num}}件</span>
            </span>
            <br />
            <span
              class="fs16 colc"
              style="padding-top:7px;display:inline-block;width:100%"
            >￥{{item.amount}}</span>
          </span>
        </div>
      </div>
    </div>
    <div id="payfor" class="mui-control-content">
      <div>
        <div class="mui-input-row mui-search" style="height:40px;">
          <input
            v-model="queryname"
            type="search"
            class="mui-input-clear"
            style="background:#fff;border-radius:0;padding-left:15px"
            placeholder="搜索"
          />
        </div>
        <div
          style="margin:10px 0;background:#fff;padding:20px"
          v-for="(item, index) in detaillists1"
          :key="index"
          @click="goiddetail(item)"
        >
          <img
            
            :src="item.imagepath"
            alt
            style="width:80px;height:80px;float:left;margin-right:15px"
          />
          <span style="display:inline-block;width:70%;padding: 10px 0;" class="detail_span fs16">
            <span class="detail_span fs16" style="width:100%">
              {{item.itemname}}
              <span style="display:inline-block;float:right">{{item.num}}件</span>
            </span>
            <br />
            <span class="fs16 colc" style="padding-top:7px;display:inline-block;width:100%">
              ￥{{item.amount}}
              <span style="float:right;z-index:100">
                <button style="background:#ccc;color:#fff" @click.stop="orderClert(item)">取消</button>
                <button style="background:rgb(0, 178, 163);color:#fff" @click.stop="orderPay(item)">去支付</button>
              </span>
            </span>
          </span>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
export default {
  layout: 'default2',
  data() {
    return {
      ishowtk: false,
      ts: this.newStore(),
      ddList: [],
      orderno: '',
      des: '',
      tel: '',
      queryname: '',
      queryname2: '',
      page: 1,
      pageSize: '10000',
      detaillists1: [],
      detaillists2: [],
      prepayid: {}
    }
  },
  watch: {
    queryname(newval, oldval) {
      console.log(newval)
      this.detaillists1 = []
      this.getorderlist()
    },
    queryname2(newval, oldval) {
      console.log(newval)
      this.detaillists2 = []
      this.getorderlist2()
    }
  },
  created() {
    // this.getddList()
    this.getorderlist()
    this.getorderlist2()
  },
  methods: {
    dosearch() {
      console.log(this.queryname)
    },
    //近详情
    goiddetail(item) {
      this.$router.push({
        name: 'forderdetail',
        query: {
          name: '我的订单',
          word: '订单详情',
          orderid: item.orderid
        }
      })
    },
    //取消订单
    orderClert(item) {
      var btnArray = ['取消', '确定']
      mui.confirm('是否确认取消订单?', '提示', btnArray, e => {
        if (e.index == 1) {
          const req = {
            token: localStorage.getItem('token'),
            orderno: item.orderno
          }
          this.spost(this.ts, '/orderc/orderClert', req).then(ts => {
            if (ts.d.flag == '200') {
              this.getorderlist()
              mui.toast('取消成功')
            }
          })
        }
      })
    },
    //支付
    orderPay(item) {
      const req = {
        token: localStorage.getItem('token'),
        orderno: item.orderno
      }
      this.spost(this.ts, '/orderc/orderPay', req).then(ts => {
        if (ts.d.flag == '200') {
          this.prepayid = ts.d.data
          this.PayMoney()
        }
      })
    },
    PayMoney() {
      if (typeof WeixinJSBridge == 'undefined') {
        if (document.addEventListener) {
          document.addEventListener(
            'WeixinJSBridgeReady',
            this.onBridgeReady,
            false
          )
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady)
          document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady)
        }
      } else {
        this.onBridgeReady()
      }
    },
    onBridgeReady() {
      WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        {
          timeStamp: `${this.prepayid.timeStamp}`, // 时间戳，自1970年以来的秒数
          appId: `${this.prepayid.appId}`, // 公众号名称，由商户传入
          nonceStr: `${this.prepayid.nonceStr}`, // 随机串
          package: `${this.prepayid.package}`, //
          signType: `${this.prepayid.signType}`, // 微信签名方式
          paySign: `${this.prepayid.paySign}` // 微信签名
        },
        res => {
          // alert(JSON.stringify(res))
          // alert(`输入密码后从微信服务器返回来的支付结果：${JSON.stringify(res)}`);
          if (res.err_msg == 'get_brand_wcpay_request:ok') {
            console.log('res.err_msg', res.err_msg)
            this.getorderlist()
            this.getorderlist2()
            // this.$router.push({ name: 'eporder' })
            // this.$router.push({
            //   name: 'chufang-payOff',
            //   query: {
            //     je_sf: this.je_sf,
            //     payResult: true,
            //     payMessage: '支付成功'
            //   }
            // })
          } else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
            // this.$router.push({ name: 'payOff', query: { je_sf: this.je_sf, payResult: false, payMessage: '取消支付' } });
            // this.showAlert = true
            // this.alertText = '已取消支付'
            console.log('res.err_msg', res.err_msg)
          } else {
            // this.$router.push({ name: 'payOff', query: { je_sf: this.je_sf, payResult: false, payMessage: res.err_msg } });
            // this.showAlert = true
            // this.alertText = `支付失败,错误信息${res.err_msg}`
            console.log('res.err_msg', res.err_msg)
          }
        }
      )
    },
    //调用列表1
    getorderlist() {
      const req = {
        token: localStorage.getItem('token'),
        paystatus: '0',
        query: this.queryname,
        page: this.page + '',
        pageSize: this.pageSize
      }
      this.spost(this.ts, '/orderc/getorderlist', req).then(ts => {
        this.detaillists1 = ts.d.data
      })
    },
    //调用列表2
    getorderlist2() {
      const req = {
        token: localStorage.getItem('token'),
        paystatus: '1',
        query: this.queryname2,
        page: this.page + '',
        pageSize: this.pageSize
      }
      this.spost(this.ts, '/orderc/getorderlist', req).then(ts => {
        this.detaillists2 = ts.d.data
      })
    }

  }
}
</script>

<style scoped>
.popdiv {
  position: fixed;
  z-index: 80;
  width: 80%;
  left: 10%;
  top: 20%;
  background: #f1f1f1;
  padding: 20px;
}
.mui-backdrop {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 998;
  background-color: rgba(0, 0, 0, 0.3);
}
.moneyinfo {
  text-align: right;
  display: inline-block;
  width: 100%;
  color: rgb(143, 143, 148);
  line-height: 20px;
}
.maincontent {
  padding: 15px;
  width: 96%;
  margin: 10px auto;
  border-radius: 5px;
}
.ddcontent {
  display: flex;
  justify-content: space-between;
}
.ddcontent:first-child {
  margin-top: 0;
}
.ddimg {
  width: 30%;
}
.ddinfo {
  width: 50%;
  padding: 0 10px;
}
.ddinfo p:first-child {
  color: #333;
  line-height: 18px;
}
.money {
  padding: 15px 0;
  padding-bottom: 6px;
  text-align: right;
  color: #333;
}
.btn {
  height: 15px;
}
.btn span {
  padding: 5px 10px;
  border-radius: 15px;
  border: 1px solid #ccc;
  margin: 0 5px;
  float: right;
  letter-spacing: 2px;
}
.detail_span {
  display: inline-block;
  line-height: 25px;
}
</style>
